﻿using INTEX.DataBase.Tools;
using INTEX.SCHEDULER;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
namespace WPFExcelReport
{
    public class VYGRAV95 : ViewModelBase
    {
        public Dictionary<string, UIAM> MachinesLib
        {
            get
            {
                return Scheduler.MachinesLib;
            }
        }
        public Dictionary<string, object> JobsLib
        {
            get
            {
                return Scheduler.JobsLib;
            }
        }
        private bool _IsShowGantt;
        public bool IsShowGantt
        {
            get { return _IsShowGantt; }
            set
            {
                _IsShowGantt = value;
                OnPropertyChanged("IsShowGantt", value);
            }
        }
 
        private bool _IsYGRAV95; public bool IsYGRAV95 { get { return _IsYGRAV95; } set { _IsYGRAV95 = value; OnPropertyChanged("IsYGRAV95", value); } }

        private DataRow _dwSTOCK; public DataRow dwSTOCK { get { return _dwSTOCK; } set { _dwSTOCK = value; OnPropertyChanged("dwSTOCK", value); } }
        private string _KSTKRS; public string KSTKRS { get { return _KSTKRS; } set { _KSTKRS = value; OnPropertyChanged("KSTKRS", value); } }
        private string _KST; public string KST { get { return _KST; } set { _KST = value; OnPropertyChanged("KST", value); } }
        private string _AGANG; public string AGANG { get { return _AGANG; } set { _AGANG = value; OnPropertyChanged("AGANG", value); } }
        private string _AMTYP; public string AMTYP { get { return _AMTYP; } set { _AMTYP = value; OnPropertyChanged("AMTYP", value); } }
        private string _JOBTYPE;
        public string JOBTYPE
        {
            get
            {
                return _JOBTYPE;
            }
            set
            {
                _JOBTYPE = value;
                OnPropertyChanged("JOBTYPE");
            }
        }
        VGSDAT VGSDAT;
        public ITXSSPA Scheduler=new ITXSSPA();
        VKDSTAMM VKDSTAMM  ;
        VVKPOS VVKPOS  ;
        VABPOS VABPOS ;
        VAMSTAMM VAMSTAMM  ;
        VVVTLIEF VVVTLIEF  ;
        VVRPOS VVRPOS  ;
        VICC_MKRF VICC_MKRF  ;
        VICC_PVKPOS VICC_PVKPOS  ;
        VVAFARBEN VVAFARBEN  ;
        VICC_ABPOS_MCO VICC_ABPOS_MCO  ;
        VICC_STOCK VICC_STOCK ;
        public DelegateCommand PlanCommand { get; set; }
       
        VICC_QUERYTB QTB  ;
        public VYGRAV95()
            : base()
        {
            VKDSTAMM = new VKDSTAMM();
            VVKPOS = new VVKPOS();
            VABPOS = new VABPOS();
            VAMSTAMM = new VAMSTAMM();
            VVVTLIEF = new VVVTLIEF();
            VVRPOS = new VVRPOS();
            VICC_MKRF = new VICC_MKRF();
            VICC_PVKPOS = new VICC_PVKPOS();
            VVAFARBEN = new VVAFARBEN();
            VICC_ABPOS_MCO = new VICC_ABPOS_MCO();
            VICC_STOCK = new VICC_STOCK();

            QTB = new VICC_QUERYTB();
            IsCostMode = true;
            IsYGRAV95 = true;

            SetEnv = new DataSet();
            SetData = new DataSet();
            SetProd = new DataSet();
            PlanCommand = new DelegateCommand(x => Plan());
           
            _dwSTOCK = VICC_STOCK.SetProd.Tables["ICC_STOCK"].NewRow();
            Scheduler = new ITXSSPA();
            Scheduler.MODE = "P"; 
        }

        
        public override void LoadEnv()
        {
            base.LoadEnv();
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataRow dwData;

            dwData = VAMSTAMM.SetEnv.Tables["AMSTAMM"].NewRow();
            dwData["AMMANDANT"] = "00";
            dwData["AMKSTKRS"] = KSTKRS;
            dwData["AMKST"] = KST;
            dwData["AMTYP"] = AMTYP;
            SResult = VAMSTAMM.QueryAMSTAMM(dwData, 1);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                row["AMBEZ"] = Helpers.Helper.GetGB2312Value(row["AMBEZ"].ToString().Trim());
                row["AMAENDZL"] = 1;//排产最后日期
                row["AMAENDDAT"] = 0;//排产最后日期
                row["AMNEUDAT"] = "0000";//排产最后时间
            }
            SetEnv.Merge(SResult);
            INTEXDBC.DeleteICC_STDMACHINE(null, 1);
            INTEXDBC.InsertICC_STDMACHINE(null, 1);
            SResult = INTEXDBC.QueryICC_STDMACHINE(dwData, 1);
            SetEnv.Merge(SResult);
            SResult = INTEXDBC.QueryICC_SRULES(null, 9999);
            SetEnv.Merge(SResult);
            SResult = INTEXDBC.QueryICC_PLANSETUP(null, 9999);
            SetEnv.Merge(SResult);
        }
        public  void ShowDetail()
        { 
            try
            {
                BuildData(null);
                //RemoveAllJobs();
                Scheduler.PrepareAllJob(false);
                Plan();
                Scheduler.ShowMachine();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        //寻找出所有的生产单号码，可以一次得到
        private DataSet YGRON(string _krs, string _lfd, string _pos, string _EXART)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataSet STemp = new DataSet();
            DataRow dwData = QTB.SetProd.Tables["ICC_QUERYTB"].NewRow();
            dwData["ABAVKAUFKRS"] = _krs;
            dwData["ABAVKAUFLFD"] = _lfd;
            dwData["ABAVKAUFPOS"] = _pos;
            SResult = INTEXDBC.QueryICC_ABAX(dwData, 4);
            SReturn.Merge(SResult);
            dwData["ABFMANDANT"] = app.Mandant;
            dwData["ABFAUFKRS"] = _krs;
            dwData["ABFAUFLFD"] = _lfd;
            dwData["ABFAUFPOS"] = _pos;
            SResult = INTEXDBC.QueryICC_MKRF(dwData, 6);
            SReturn.Merge(SResult);
            string key = _krs + _lfd + _pos + ";";
            string processed = key;
            bool IsRun = false;
            while (true)
            {
                IsRun = false;
                foreach (DataRow row in SReturn.Tables["ICC_ABAX"].Rows)
                {
                    string ABAAUFTYP = row["ABAAUFTYP"].ToString();
                    _krs = row["ABAAUFKRS"].ToString();
                    _lfd = row["ABAAUFLFD"].ToString();
                    _pos = row["ABAAUFPOS"].ToString();
                    key = _krs + _lfd + _pos + ";";
                    if (ABAAUFTYP == "N")
                        continue;
                    if (processed.Contains(key))
                        continue;
                    processed += key;
                    dwData["ABAVKAUFKRS"] = _krs;
                    dwData["ABAVKAUFLFD"] = _lfd;
                    dwData["ABAVKAUFPOS"] = _pos;
                    SResult = INTEXDBC.QueryICC_ABAX(dwData, 4);
                    STemp.Merge(SResult);
                    dwData["ABFMANDANT"] = app.Mandant;
                    dwData["ABFAUFKRS"] = _krs;
                    dwData["ABFAUFLFD"] = _lfd;
                    dwData["ABFAUFPOS"] = _pos;
                    SResult = INTEXDBC.QueryICC_MKRF(dwData, 6);
                    SReturn.Merge(SResult);
                    /*
                    SResult = INTEXDBC.QueryABFOLGE(dwData, 1);
                    SReturn.Merge(SResult);
                    SResult = INTEXDBC.QueryKSLEIST(dwData, 3);
                    SReturn.Merge(SResult);*/
                    IsRun = true;
                }
                SReturn.Merge(STemp);
                if (!IsRun)
                    break;
            }
            return SReturn;
        }

       
        public  void GetOrderNetWork(string _krs, string _lfd, string _pos)
        { 
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataSet STemp = new DataSet();
            DataRow dwData = QTB.SetProd.Tables["ICC_QUERYTB"].NewRow();
            string EXART = _krs + "-" + Helpers.Helper.Right(_lfd, 4) + "-" + _pos;
            SReturn = YGRON(_krs, _lfd, _pos, EXART);
            SetData.Clear();
            SetData.Merge(SReturn);
        }
        public  void BuildData(DataRow dwInput)
        { 
            try
            {
                GetOrderNetWork(KRS, LFD, POS);
                SetEnv.Tables["ICC_MKRF"].Clear();
                if (SetData.Tables.Contains("ICC_MKRF"))
                {
                    SetEnv.Tables["ICC_MKRF"].Merge(SetData.Tables["ICC_MKRF"]);
                }
                if (SetData.Tables.Contains("ABFOLGE"))
                {
                    if (SetEnv.Tables.Contains("ABFOLGE"))
                    {
                        SetEnv.Tables["ABFOLGE"].Clear();
                    }
                    SetEnv.Merge(SetData.Tables["ABFOLGE"]);
                }
                if (SetData.Tables.Contains("KSLEIST"))
                {
                    if (SetEnv.Tables.Contains("KSLEIST"))
                    {
                        SetEnv.Tables["KSLEIST"].Clear();
                    }
                    SetEnv.Merge(SetData.Tables["KSLEIST"]);
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void Plan()
        {
            fHandle = new ViewModelBase.AsyncRunHandle(Scheduler.StartPlan);
            AsyncRun(fHandle, "StartPlan", SetProd, SetEnv, SetData, MachinesLib, JobsLib);
        }
        public void TryLoad()
        {
            fHandle = new ViewModelBase.AsyncRunHandle(YGRPLANDB);
            AsyncRun(fHandle, "YGRPLANDB", null, null, null);
        }
        public override void CallbackImpl(IAsyncResult run, DataSet _SResult)
        {
            base.CallbackImpl(run, _SResult);
            AsyncRunHandle handle = run.AsyncState as AsyncRunHandle;
            switch (handle.Method.Name)
            {
                case "StartPlan": 
                    break;
                case "YGRPLANDB":
                case "JLTPLANDB":
                   // LoadVKPList();
                    break;
                default:
                    break;
            }
        }

        public DataSet YGRPLANDB(object _KSTKRS = null, object _KST = null, object _ABFAGANG = null, object p4 = null, object p5 = null)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            DataRow dwData = null;
            string SetDate = "";
            try
            {
                return Return;
                dwData = QTB.SetProd.Tables["ICC_QUERYTB"].NewRow();
                dwData["VKPMANDANT"] = "00";

                VICC_PVKPOS.InsertICC_PVKPOS(dwData, 2);//INSERT  纱线

                dwData = QTB.SetProd.Tables["ICC_QUERYTB"].NewRow();
                dwData["VKPMANDANT"] = "00";
                VICC_PVKPOS.UpdateICC_PVKPOS(dwData, 11);//更新销售单信息  
                VICC_PVKPOS.UpdateICC_PVKPOS(dwData, 21);//订单行汇总                   

                dwData = QTB.SetProd.Tables["ICC_QUERYTB"].NewRow();
                dwData["ABFMANDANT"] = "00";
                dwData["ABFKSTKRS"] = (string)_KSTKRS;
                dwData["ABFKST"] = (string)_KST;
                dwData["MKFERLKZ"] = " ";
                dwData["ABFAGANG"] = (string)_ABFAGANG;
                //INTEXDBC.DeleteMKRF(dwData, 2);
                //INTEXDBC.DeleteICC_MKRF(dwData, 2); 
                VICC_MKRF.InsertICC_MKRF(dwData, 4);//插入所有生产单进入待排产的记录
                VICC_MKRF.InsertICC_MKRF(dwData, 5);//插入所有销售进入待排产的记录
                //INTEXDBC.UpdateICC_MKRF(dwData, 7);//MACHINEID
                VICC_MKRF.UpdateICC_MKRF(dwData, 9);//ABPEALFM,ABPEAKG,ABPERLDAT,ABPERLKZ 
                dwData["C3"] = 713;
                VICC_MKRF.UpdateICC_MKRF(dwData, 8);//GAKG1....GAKG8 
                dwData["C3"] = 610;
                VICC_MKRF.UpdateICC_MKRF(dwData, 8);//GAKG1....GAKG8 
                VICC_MKRF.UpdateICC_MKRF(dwData, 10);//ABARESMG
                VICC_MKRF.UpdateICC_MKRF(dwData, 11);//ABFISTARTDAT,ABFISTARTZT,ABFIENDDAT,ABFIENDZT    

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }
    }
}